package view;

import Bean.Room;
import control.RoomManagerControl;
import java.awt.HeadlessException;
import java.sql.SQLException;
import java.util.List;
import javax.swing.JOptionPane;

public class AddRoomDialog extends RootDialog
{

    private RoomManagerControl rmControl = new RoomManagerControl();
    private RoomCategoryPanel panel;
    private String choice = null;

    public AddRoomDialog(view.RoomCategoryPanel panel)
    {

        initComponents();
        this.panel = panel;
        this.setTitle("Thêm phòng");
        this.reset();
        Init();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jCBLoaiphong = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jBThem = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jTFTenphong = new javax.swing.JTextField();
        jTFGiaphong = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 153));
        jLabel1.setText("Loại phòng");

        jCBLoaiphong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jCBLoaiphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCBLoaiphongActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 51, 153));
        jLabel2.setText("Giá phòng");

        jBThem.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jBThem.setText("Thêm");
        jBThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBThemActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 51, 153));
        jLabel3.setText("Tên phòng");

        jTFTenphong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jTFTenphong.setText(" ");
        jTFTenphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFTenphongActionPerformed(evt);
            }
        });

        jTFGiaphong.setEditable(false);
        jTFGiaphong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jTFGiaphong.setText(" ");

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/add_room.png"))); // NOI18N

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(0, 51, 153));
        jLabel5.setText("VNĐ/ngày");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(1, 1, 1)
                .addComponent(jLabel4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTFTenphong)
                            .addComponent(jTFGiaphong)
                            .addComponent(jCBLoaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addComponent(jBThem, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFTenphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCBLoaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTFGiaphong, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(45, 45, 45)
                .addComponent(jBThem, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 11, Short.MAX_VALUE)
                .addComponent(jLabel4))
        );

        jTFGiaphong.getAccessibleContext().setAccessibleName("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCBLoaiphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBLoaiphongActionPerformed
        choice = (jCBLoaiphong.getSelectedItem().toString());
        jTFGiaphong.setText(Integer.toString(rmControl.getPriceOf(choice)));

    }//GEN-LAST:event_jCBLoaiphongActionPerformed

    private void jBThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBThemActionPerformed
        Room Room = new Room();
        Room.setTyperoom((String) (jCBLoaiphong.getSelectedItem()));
        Room.setCostroom(Integer.parseInt(jTFGiaphong.getText()));
        Room.setState("Còn trống");
        try
        {
            Room.setName(Integer.parseInt(jTFTenphong.getText()));
            if (rmControl.TestInforRoom(Room.getName()) == false)
            {
                JOptionPane.showMessageDialog(null, "Phòng đã tồn tại");
            }
            else
            {
                if ((Room.getName() <= 0) || (jCBLoaiphong.getSelectedItem() == null))
                {
                    JOptionPane.showMessageDialog(null, "Hãy nhập tên phòng là 1 số lớn hơn 0");
                }
                else
                {
                    rmControl.AddRoom(Room);
                    JOptionPane.showMessageDialog(null, "Nhập thành công");
                    panel.reset();
                    jTFTenphong.setText(null);
                }
            }
        }
        catch (NumberFormatException | SQLException | HeadlessException ex)
        {
            JOptionPane.showMessageDialog(null, "Hãy nhập tên phòng là 1 số lớn hơn 0");
        }

    }//GEN-LAST:event_jBThemActionPerformed

    private void jTFTenphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFTenphongActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTFTenphongActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBThem;
    private javax.swing.JComboBox jCBLoaiphong;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTFGiaphong;
    private javax.swing.JTextField jTFTenphong;
    // End of variables declaration//GEN-END:variables

    private void reset()
    {
        jTFTenphong.setText(null);
        jTFGiaphong.setText(null);
        jCBLoaiphong.removeAllItems();
        List<Room> list = rmControl.getRoomTypeName();
        for (Room obj_room : list)
        {
            jCBLoaiphong.addItem(obj_room.getTyperoom().toString());
        }
    }
}
